package cn.openea.perpartner.crm.web.controller;

import cn.openea.perpartner.common.core.constant.UserConstants;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.crm.util.UserContext;
import cn.openea.perpartner.crm.web.entity.CrmMenu;
import cn.openea.perpartner.crm.web.service.ICrmMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.openea.perpartner.common.core.web.domain.AjaxResult.warn;

/**
 * 菜单权限表(CrmMenu)表控制层
 *
 * @author Weird077
 * @since 2022-12-19 22:30:36
 */
@Api("菜单权限")
@RestController
@RequestMapping("/crm/menu")
public class CrmMenuController extends BaseController {

    @Autowired
    private ICrmMenuService menuService;
    @ApiOperation("菜单列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CrmMenu menu) {
        List<CrmMenu> list = menuService.selectMenuList(menu, UserContext.getUserId());
        return getDataTable(list);
    }
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId)
    {
        return success(menuService.selectMenuById(menuId));
    }
    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(CrmMenu menu)
    {
        List<CrmMenu> menus = menuService.selectMenuList(menu, UserContext.getUserId());
        return success(menuService.buildMenuTreeSelect(menus));
    }
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        List<CrmMenu> menus = menuService.selectMenuList(UserContext.getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ajax;
    }
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody CrmMenu menu)
    {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
        {
            return error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setCreateUser(UserContext.getUserId());
        menu.setUpdateUser(UserContext.getUserId());
        return toAjax(menuService.insertMenu(menu));
    }
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody CrmMenu menu)
    {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu)))
        {
            return error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        else if (menu.getMenuId().equals(menu.getParentId()))
        {
            return error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menu.setUpdateUser(UserContext.getUserId());
        return toAjax(menuService.updateMenu(menu));
    }
    @DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId)
    {
        if (menuService.hasChildByMenuId(menuId))
        {
            return warn("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId))
        {
            return warn("菜单已分配,不允许删除");
        }
        return toAjax(menuService.deleteMenuById(menuId));
    }
}

